<template>
    <div>
        <!-- <el-form-item class="table-full-row" label="文件类型" prop="doctypecode" :rules="newRule('文件类型', 'required')">
            <select-type v-model="model.doctypecode" :dicname.sync="model.doctypename" style="width:100%;"></select-type>
        </el-form-item> -->

        <el-form-item v-for="(item, index) in formData" :key="index" :label="item.dicname" :prop="item.diccode" :rules="newRule(item.dicname, 'required')">
            <my-cascader v-model="model[item.diccode]" 2way="dicname" :dicname.sync="model[`${item.diccode}_name`]" :data="item.children"
                :props="{value:'diccode', label:'dicname',}" @change="changeHandler(item.diccode)"></my-cascader>
        </el-form-item>

        <slot name="sonKey"></slot>
    </div>

</template>

<script>
import MODEL_MIXIN from '@mixins/model';

import SelectType from './select-type';

export default {
    mixins: [MODEL_MIXIN],
    components: {
        SelectType,
    },
    data() {
        return {
            formData: [],

            taglistkeys: [],
        };
    },
    watch: {
        'model.taglist': {
            handler(val) {
                if (!val) {
                    return;
                }

                val.forEach((item) => {
                    this.model[item.parentcode] = item.tagid;
                });
            },
            deep: true,
        },
    },
    mounted() {
        this.queryData(this.model.doctypecode);
    },
    methods: {
        queryData(rowguid) {
            this.taglistkeys = [];
            this.formData = [];
            if (!rowguid) {
                this.model.taglist = [];
                return;
            }
            // file/platformmg/filetype/getdic.json
            this.$get(
                `${this.$store.state.api.fileUrl}/platformmg/filetype/getdic.json`,
                {
                    rowguid,
                },
                (data) => {
                    this.formData = data;
                    data.forEach((item) => {
                        this.model[item.diccode] = this.model[item.diccode] || '';
                        this.taglistkeys.push(item.diccode);
                    });
                }
            );
        },

        changeHandler(code) {
            this.$emit('changeCode',code)
            var taglist = [];
            this.taglistkeys.forEach((key) => {
                taglist.push({
                    tagid: this.model[key],
                    tagname: this.model[`${key}_name`],
                    parentcode: key,
                });
            });
            this.model.taglist = taglist;
        },
    },
};
</script>